<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\common\model as M;

/**
 * 提醒跟进
 */
class Noticeprocesson extends Command
{
    protected function configure()
    {
        $this->setName('noticeprocesson')
            ->setDescription('提醒跟进');
    }

    protected function execute(Input $input, Output $output)
    {
        ## 查询30天以上未跟进的
        ## 注意：只需要条件用=即可，不需要用>=，因为此脚本每天都会执行，所以对应天数分别只命中一次即可，下同。
        $users = M\User::query("
            SELECT * FROM (
                SELECT uid,spread_suid,sales_admin_id,full_name,nickname,mobile,last_processon_time,DATEDIFF(NOW(),last_processon_time) as gap FROM eb_user WHERE last_processon_time is not null
            ) AS t1 WHERE t1.gap = 30
        ");
        if ( !empty($users) ) {
            foreach ( $users as $user ) {
                ## 记录到未跟踪记录中 01-先查询是否有记录 02-如果没有记录再加入
                $record = M\UnfollowRecord::where('uid' , $user['uid'])->count();
                if ( empty($record) ) {
                    M\UnfollowRecord::create([
                        'uid'           => $user['uid'],
                        'sales_admin_id'=> $user['sales_admin_id'],
                        'spread_suid'   => $user['spread_suid'],
                        'record_time'   => date('Y-m-d H:i:s'),
                        'gap_days'      => 30,
                        'handle_status' => 0
                    ]);
                }

                $this->generateMessageTaskForProcesson(30 , $user);
            }
        }

        ## 查询15天以上未跟进的
        $users = M\User::query("
            SELECT * FROM (
                SELECT uid,spread_suid,sales_admin_id,full_name,nickname,mobile,last_processon_time,DATEDIFF(NOW(),last_processon_time) as gap FROM eb_user WHERE last_processon_time is not null
            ) AS t1 WHERE t1.gap = 15
        ");
        if ( !empty($users) ) {
            foreach ( $users as $user ) {
                $this->generateMessageTaskForProcesson(15 , $user);
            }
        }

        ## 查询7天以上未跟进的
        $users = M\User::query("
            SELECT * FROM (
                SELECT uid,spread_suid,sales_admin_id,full_name,nickname,mobile,last_processon_time,DATEDIFF(NOW(),last_processon_time) as gap FROM eb_user WHERE last_processon_time is not null
            ) AS t1 WHERE t1.gap = 7
        ");
        if ( !empty($users) ) {
            foreach ( $users as $user ) {
                $this->generateMessageTaskForProcesson(7 , $user);
            }
        }
    }

    /**
     * 给销售发送其名下明日将要过生日的客户提醒
     * @Author   FW
     * @DateTime 2024-08-06
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    private function generateMessageTaskForProcesson($days , $user)
    {
        $summary = sprintf("您有客户已满%s天未跟进了，请及时跟进。" , $days);
        $content = sprintf('
            <div>
                <div class="flex">
                    <div style="width: 120rpx;">客户UID</div>
                    <div>%d</div>
                </div>
                <div class="flex">
                    <div style="width: 120rpx;">客户姓名</div>
                    <div>%s</div>
                </div>
                <div class="flex">
                    <div style="width: 120rpx;">客户昵称</div>
                    <div>%s</div>
                </div>
                <div class="flex">
                    <div style="width: 120rpx;">手机号码</div>
                    <div>%s</div>
                </div>
                <div class="flex">
                    <div style="width: 120rpx;">上次跟进</div>
                    <div>%s</div>
                </div>
            </div>
            <div style="font-size: 22rpx; color: #999; margin-top:20rpx">备注：系统会分别在距离上次跟进的第15天、30天以及60天提醒一次</div>
        ' , $user['uid'] , ($user['full_name'] ?? '未实名'), $user['nickname'] , $user['mobile'] , $user['last_processon_time']);

        $cycle   = M\Message::where([
            'mtype'      => M\Message::MSG_TYPE_PROCESSON,
            'mgap_days'  => 1,
            'uid'        => $user['spread_suid'],
        ])->value('cycle');

        M\Message::saveOne([
            'title'         => '客户跟进提醒',
            'summary'       => $summary,
            'content'       => htmlspecialchars($content),
            'mtype'         => M\Message::MSG_TYPE_PROCESSON,
            'mgap_days'     => 1,
            'uid'           => $user['spread_suid'],
            'cycle'         => ($cycle+1),
            'send_time'     => date('Y-m-d H:i:s'),
        ]);
    }
}// Class End